package com.tongji.diy.model.furniturePlacementOptimization
{
	import com.tongji.diy.model.RoomDataVO;
	import com.tongji.diy.model.furniturePlacementOptimization.assembleComplement.Bedding;
	import com.tongji.diy.model.furniturePlacementOptimization.assembleComplement.FacalCompound;
	import com.tongji.diy.model.furniturePlacementOptimization.assembleComplement.FurnitureBase;
	import com.tongji.diy.model.furniturePlacementOptimization.assembleComplement.NearBase;
	import com.tongji.diy.model.furniturePlacementOptimization.assembleComplement.SLbase;
	
	import flash.geom.Point;
	import flash.geom.Vector3D;

	public class BlockLayoutConstructor
	{
		//临时使用public
		public var data:DataAndFunction = new DataAndFunction;
		private var compound:ConstructorCompound;
		private var iteration:ConstructorIteration;
		private var builder:ConstructorBuilder;
		
		public function BlockLayoutConstructor()
		{
			super();
		}
		
		public function init(room:RoomDataVO,block:Vector.<FurnitureBase>):void
		{
			data._block = block;
			data._room = room;
			
			builder = new ConstructorBuilder(data);
			//进行房间家具组合，第一步将facal进行组装并靠墙摆放
			compound = new ConstructorCompound(data);
			iteration = new ConstructorIteration(data);
			nearbyBoundingBoxesUpdate();
		}
		//更新nearby包围盒
		private function nearbyBoundingBoxesUpdate():void
		{
			var list:Vector.<NearBase> = new Vector.<NearBase>;
			if(data._seatNearbyList.length)
			{
				list = data._seatNearbyList;
			}
			else
			{
				list = data._beddingNearbyList;
			}
			
			var i:int = 0;
			//先清空所有包围盒
			data._boundBoxes.splice(0,data._boundBoxes.length);
			//加入furnitureBlock包围盒
			data._boundBoxes.push(data._furnitureBlock.accessibleBox);
			//加入其它nearby包围盒
			while(i < list.length)
			{
				data._boundBoxes.push(list[i].accessibleBox);
				++i;
			}
		}
		
		public function assignNearby():void
		{
			if(OptimizationParameter.DEBUG)
			{
				//test edition
				if(data.perFrameData.used == false)
					iteration.assignNearby2();
				else
					iteration.assignNearby2(data.perFrameData);
			}
			else
			{
				iteration.assignNearby();
			}
			
			nearbyBoundingBoxesUpdate();
		}
		
		public function get showList():Vector.<FurnitureBase>
		{
			var v:Vector.<FurnitureBase> = new Vector.<FurnitureBase>;
			for(var i:int = 0; i < data._seatNearbyList.length; ++i)
			{
				v.push(data._seatNearbyList[i]);
			}
			
			for(i = 0; i < data._beddingList.length; ++i)
			{
				v.push(data._beddingList[i]);
			}
	
			for(i = 0; i < data._beddingNearbyList.length; ++i)
			{
				v.push(data._beddingNearbyList[i]);
			}
			
			for(i = 0; facalBlock && i < facalBlock.length; ++i)
			{
				v.push(facalBlock[i]);
			}
			return v;
		}	
		
		public function get facalBlock():Vector.<FurnitureBase>
		{
			if(!data._furnitureBlock)
				return null;
			var count:int = 0;
			var v:Vector.<FurnitureBase> = data._furnitureBlock.furVector;
			if(v && !v.every(facalCompoundExist))
			{
				v.push((v[count] as FacalCompound).containee,(v[count] as FacalCompound).container);
				v.splice(count,1);
			}
			
			return v;
			
			function facalCompoundExist(item:Object, index:int, v:*):Boolean
			{
				if(item is FacalCompound)
				{
					count = index;
					return false;
				}
				return true;
			}
		}
				
		public function reConstruct():void
		{
			var collisionRoom:Boolean = false;
			while(!collisionRoom)
			{
				var v:Vector3D = data.searchLocationWall(data._furnitureBlock,Math.random());
				
				data._furnitureBlock.x = v.x;
				data._furnitureBlock.y = v.y;
				data._furnitureBlock.angle =v.z;
				
				if(data.boundingBoxInRoom(data._furnitureBlock.boundingBox, data._room))
				{
					collisionRoom = true;
				}
			}
			nearbyBoundingBoxesUpdate();
		}
		//获得该block中所有的包围盒
		public function get boundingBoxes():Vector.<Vector.<Point>>
		{
			return data._boundBoxes;
		}
		
		public function get furnitureBlock():Vector.<Point>
		{
			return data._furnitureBlock.accessibleBox;
		}
		
		public function get blockPeopleNum():int
		{
			var count:int = 0;
			var list:Vector.<FurnitureBase>;
			if(data._beddingList)
				list = data._beddingList;
			else
				list = data._seatList;
			for(var i:int = 0; i < list.length; ++i)
			{
				count += (list as SLbase).peopleNum;
			}
			return count;
		}
	}	
}